package com.adou.jdbc;

import com.adou.jdbc.pojo.Account;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

/**
 * @description:JDBC DQL
 * @author:
 * @version: JDBCDemo.java v 0.1
 * @date: 2023.04.01
 */
public class JDBCDemo5 {
    public static void main(String[] args) throws Exception {
        // 1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        // 2.获取连接

        String url = "jdbc:mysql://127.0.0.1:3306/db1";
        String username = "root";
        String password = "root";
        Connection connection = DriverManager.getConnection(url, username, password);

        // 3.sql
        String sql = "select * from account";

        // 4。获取执行sql对象
        Statement statement = connection.createStatement();

        // 5.执行sql
        ResultSet resultSet = statement.executeQuery(sql);

        ArrayList<Account> accounts = new ArrayList<>();
        // 6.处理结果，遍历rs
        while (resultSet.next()){
            // 6.1 获取数据
            int id  = resultSet.getInt(1);
            String name  = resultSet.getString(2);
            double money = resultSet.getDouble(3);

            Account account = new Account(id, name, money);
            accounts.add(account);


        }
        System.out.println(accounts);

        // 7.释放资源
        resultSet.close();
        statement.close();
        connection.close();





    }

}
